package view;


import controller.CartController;
import controller.GoodsController;
import controller.UserController;
import model.Goods;
import model.common.Entity;
import model.common.Msg;
import utils.JSONUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsClient extends Client {

    /**
     * 商品控制器类
     */
    private final GoodsController goodsController = new GoodsController();
    /**
     * 商品展示时，关联序号和商品对象
     */
    Map<String, Goods> code2Goods = new HashMap<>();

    String goodsListJson = goodsController.getGoodsList();

    private final CartController cartController;
    /**
     * 商品实体类
     */
    private Goods goods;

    public GoodsClient() {
        cartController = new CartController();
        goods = new Goods();
    }

    /**
     * 商品管理首页
     * <p>
     * 商品页面跳转控制器：
     * 本模块的操作由此方法控制跳转
     * 其它模块的操作返回上层，由Client控制跳转
     *
     * @return 返回公共操作
     */
    public String index() {
        UserController userController = new UserController();
        //System.out.println("当前用户：" + new UserController().getLoginUser());
        if (userController.getLoginUser() != null) {

            String result = null;
            if ("ADMIN".equals(userController.getLoginUser().getRole())) {
                result = userOperate("请选择你的操作", "U:用户管理", "G:商品管理", "I:首页", "IM:数据导入");
                if (result.equals(USER)) {
                    return USER;
                } else if (result.equals(EXIT)) {
                    return EXIT;
                } else if (result.equals(INDEX)) {
                    return INDEX;
                } else if (result.equals(GOOD)) {
                    return GOOD;
                } else if (result.equals(IMPORT)) {
                    return IMPORT;
                }
            } else if ("NORMAL".equals(userController.getLoginUser().getRole())) {
                result = userOperate("请选择你的操作", "UN:个人中心", "GN:商品", "C:购物车", "FN:订单", "I:首页");
                if (result.equals(UN)) {
                    return UN;
                } else if (result.equals(CART)) {
                    return CART;
                } else if (result.equals(GN)) {
                    return GN;
                } else if (result.equals(ORDER)) {
                    return ORDER;
                }
            }

        }
        showGoodsList(); // 展示商品列表
        String result = userOperate("请根据序号查看商品详情", "L:登录", "I:首页", "R:密码找回");

        while (true) {
            if (result.equals(LOGIN)) {
                return LOGIN;
            } else if (result.equals(EXIT)) {
                return EXIT;
            } else if (result.equals(INDEX)) {
                return INDEX;
            } else if (result.equals(RETURN)) {
                return RETURN;
            }

            if (result.equals(CART)) {
                // 如果上一次操作为登录 才可以访问购物车
                if (new UserController().getLoginUser() != null) {
                    return CART;
                } else {
                    System.out.println("请先登录");
                    result = userOperate("请根据序号查看商品详情", "L:登录", "I:首页");
                }
            }
            if (result.equals(FIND)) {
                // 如果上一次操作为登录 才可以访问购物车
                if (new UserController().getLoginUser() != null) {
                    return FIND;
                } else {
                    System.out.println("请先登录");
                    result = userOperate("请根据序号查看商品详情", "L:登录", "I:首页");
                }
            }


            Goods goods = code2Goods.get(result);//针对商品序号
            if (goods != null) //查看详情
            {
                //currentGoods保存选定商品
                currentGoods = goods;
                showGoodsDetail();
                result = userOperate("输入A加入购物车", "L:登录", "I:首页", "C:查看购物车");
                if (new UserController().getLoginUser() != null & result.equals(ADD)) {
                    return ADD;//加入购物车
                } else if (result.equals(CART)) {
                    cartController.showCart();
                } else {
                    System.out.println("请先登录");
                    result = userOperate("请根据序号查看商品详情", "L:登录", "I:首页");
                }
            }

        }
    }

    /**
     * 展示商品列表
     * 1.向后台发送请求，获取商品数据
     * 2.解析响应消息字符串
     * 3.展示商品列表
     */
    public void showGoodsList() {
        //1.向后台发送请求，获取商品数据
        String msg = goodsController.getGoodsList();
        //2.解析响应消息字符串
        Msg msgObj = JSONUtil.JSON2Entity(msg, Msg.class);
        Object obj = msgObj.getObj();   // [{}，{}]
        List<?> goodsList0bj = (List<?>) obj;
        System.out.println("【商品列表】");
        System.out.println("编号\t\t商品名称\t\t单价\t\t库存");
        System.out.println(" ----------------------------------");

        int index = 1;//编号
        for (Object o : goodsList0bj) {
            // o : {...}
            String goodsJson = o.toString();// {，，,}
            Goods goods = JSONUtil.JSON2Entity(goodsJson, Goods.class);
            String num = goods.getNumber() + "";//库存
            String name = goods.getName();//名称
            String price = goods.getPrice() + "";//单价
            // 3.展示商品列表
            System.out.println(index + ".\t\t" + name + "\t\t\t" + price + "\t\t" + num);
            code2Goods.put(index + "", goods);
            index++;

        }
    }

    /**
     * 查看商品详情
     * 1.通过数据ID获取数据，然后进行展示
     * 2.在展示商品的时候，把商品列表存储起来，然后，当选择商品编号，就把对应的商品展示出来
     * Map<String, Goods> :key --> 编号，value --> Goods对象
     */
    public void showGoodsDetail() {
        System.out.println("【商品详情】");
        System.out.println("编号\t\t商品名称\t\t单价\t\t库存\t\t品牌");
        System.out.println("----------------------------------");
        String num = currentGoods.getNumber() + "";//库存
        String name = currentGoods.getName();//名称
        String price = currentGoods.getPrice() + "";//单价
        String brand = currentGoods.getBrand();//品牌

        // 3.展示商品列表
        System.out.println(1 + ".\t\t" + name + "\t\t\t" + price + "\t\t" + num + "\t\t" + brand);

    }

    public String merchandiseControl() {
        System.out.println("================商品管理模块===============");
        String result = userOperate("请输入你的操作", "1:创建商品", "2:删除商品", "3:修改商品", "4:查看商品列表", "5:查看详情", "I:首页");
        while (true) {
            if (result.equals("1")) {
                createGoods();
                return GOOD;
            } else if (result.equals("2")) {
                deleteGoods();
                return GOOD;
            } else if (result.equals("3")) {
                updateGoods();
                return GOOD;
            } else if (result.equals("4")) {
                showGoodsList();
                return GOOD;
            } else if (result.equals("5")) {
                String r = userOperate("请输入商品编号");
                Goods goods = code2Goods.get(r);
                if (goods != null) {
                    currentGoods = goods;
                }
                showGoodsDetail();
                return GOOD;
            }
            if (result.equals(EXIT)) return EXIT;
            if (result.equals(INDEX)) return INDEX;
        }
    }

    public void createGoods() {
        System.out.println("请输入商品名称:");
        String name = sc.next();
        goods.setName(name);
        System.out.println("请输入商品的价格:");
        double price = sc.nextDouble();
        goods.setPrice(price);
        System.out.println("请输入商品数量:");
        int number = sc.nextInt();
        goods.setNumber(number);
        System.out.println("请输入品牌:");
        String brand = sc.next();
        goods.setBrand(brand);
        goods.setId(Entity.getUUID());
        goods.setCreateTime(new Date());
        goodsController.addGoods(goods);
        System.out.println("创建商品成功");
    }

    public void deleteGoods() {
        showGoodsList();
        String result = userOperate("请输入要删除的商品编号");
        Goods goods = code2Goods.get(result);
        goodsController.deleteGoods(goods.getId());
        System.out.println("删除商品成功");
    }

    public void updateGoods() {
        showGoodsList();
        String result = userOperate("请输入要更新的商品编号");
        Goods goods = code2Goods.get(result);
        String r = userOperate("请输入要更新的商品信息", "1:商品名称", "2:商品价格", "3:商品数量", "4:商品品牌");
        if (r.equals("1")) {
            System.out.println("输入商品名称");
            String name = sc.next();
            goods.setName(name);
        } else if (r.equals("2")) {
            System.out.println("请输入商品价格");
            double price = sc.nextDouble();
            goods.setPrice(price);
        } else if (r.equals("3")) {
            System.out.println("请输入商品数量");
            int number = sc.nextInt();
            goods.setNumber(number);
        } else if (r.equals("4")) {
            System.out.println("请输入商品品牌");
            String brand = sc.next();
            goods.setBrand(brand);
        }
        goodsController.updateGoodsById(goods.getId(), goods);
        System.out.println("更新成功");
    }


}
